import Vue from 'vue'
import Vuex from 'vuex'
import {register} from '@/api/register'
import {getUerInfo,logout} from '@/api/user'
import {login} from '@/api/login'

Vue.use(Vuex);

export default new Vuex.Store({

  actions: {
    //注册
    register({commit}, user) {
      //返回Promise 对象代表了未来将要发生的事件，用来传递异步操作的消息。
      return new Promise((resolve, reject) => {
        // 异步处理
        // 当异步代码执行成功时，我们才会调用resolve(...), 当异步代码失败时就会调用reject(...)
        register(user).then((res) => {
          if(res.data.success){
            //拿到token
            commit('SET_TOKEN', res.data.data)
            //存储在本地
            localStorage.token = res.data.data
            //成功的方法,对应外面.then
            resolve()
          }else{
            //失败的方法,对应外面.catch
            reject(res.data.msg)
          }
        }).catch((error) => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    getUserInfo({commit, state}) {

      let that = this
      return new Promise((resolve, reject) => {
        //根据token判断
        getUerInfo(state.token).then(res => {
          if (res.data.success) {
            //设置用户信息
            commit('SET_ACCOUNT', res.data.data.account)
            commit('SET_NAME', res.data.data.nickname)
            commit('SET_AVATAR', res.data.data.avatar)
            commit('SET_ID', res.data.data.id)
            //返回用户信息
            resolve(res.data)
          } else {
            commit('SET_ACCOUNT', '')
            commit('SET_NAME', '')
            commit('SET_AVATAR', '')
            commit('SET_ID', '')
            localStorage.removeItem('token')
            resolve(data)
          }

        }).catch(error => {
          commit('SET_ACCOUNT', '')
          commit('SET_NAME', '')
          commit('SET_AVATAR', '')
          commit('SET_ID', '')
          localStorage.removeItem('token')
          reject(error)
        })
      })
    },

    // 退出
    logout({commit, state}) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(res => {
          if(res.data.success){

            commit('SET_TOKEN', '')
            commit('SET_ACCOUNT', '')
            commit('SET_NAME', '')
            commit('SET_AVATAR', '')
            commit('SET_ID', '')
            localStorage.removeItem('token')
            resolve()
          }

        }).catch(error => {
          reject(error)
        })
      })
    },
    //登入
    login({commit}, user) {
      return new Promise((resolve, reject) => {
        login(user).then(res => {
          if(res.data.success){
            commit('SET_TOKEN', res.data.data)
            localStorage.token=res.data.data;
            resolve()
          }else{
            reject(res.data.msg)
          }
        }).catch(error => {
          reject(error)
        })
      })
    }



  },

  mutations: {
    //设置token
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    //设置数量
    SET_ACCOUNT: (state, account) => {
      state.account = account
    },
    //设置名字
    SET_NAME: (state, name) => {
      state.name = name
    },
    //设置头像
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    //设置id
    SET_ID: (state, id) => {
      state.id = id
    }
  },


  state: {
    id: '',
    account: '',
    name: '',
    avatar: '',
    token: localStorage.token,
  },
})
